package com.java.task15.work3;

import java.util.*;

public class StudentMapTest {
    public static void main(String[] args) {
        //创建Map集合
        Map<Student,String> map = new HashMap<>();
        //添加元素
        map.put(new Student("张三",18),"襄阳");
        map.put(new Student("李四",19),"武汉");
        map.put(new Student("王五",20),"十堰");
        //使用toString方法遍历
        System.out.println(map);
        System.out.println("------------------------------------------------");
        //使用keySet方法使用 for-each 循环遍历
        Set<Student> s1 = map.keySet();
        for (Student tt:s1) {
            System.out.println(tt+map.get(tt));
        }
        System.out.println("------------------------------------------------");
        //使用entrySet方法然后使用 for-each 循环遍历
        Set<Map.Entry<Student, String>> t1 = map.entrySet();
        for (Map.Entry<Student, String> ta:t1) {
            System.out.println(ta);
        }
        System.out.println("------------------------------------------------");
        //使用迭代器（Iterator）遍历
        Iterator<Map.Entry<Student, String>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<Student, String> entry = entries.next();
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key +  value);
        }
    }
}
